package com.xiaoyu.im.datamodel;

import androidx.annotation.IntDef;
import androidx.annotation.StringDef;
import android.util.SparseArray;

import java.util.HashMap;
import java.util.Map;

public class MessageIdType {

    public static final int INT_LOCAL = 1;
    public static final int INT_LEAN = 2;
    public static final int INT_NIM = 3;
    public static final int INT_TIM = 4;

    public static final String STR_LOCAL = "local";
    public static final String STR_LEAN = "lean";
    public static final String STR_NIM = "nim";
    public static final String STR_TIM = "tim";

    @IntDef({INT_LOCAL, INT_LEAN, INT_NIM, INT_TIM})
    public @interface MessageIdTypeIntDef {
    }

    @StringDef({STR_LOCAL, STR_LEAN, STR_NIM, STR_TIM})
    public @interface MessageIdTypeStringDef {
    }

    private static final Map<String, Integer> STR_TO_INT = new HashMap<>();
    private static final SparseArray<String> INT_TO_STR = new SparseArray<>();

    static {
        STR_TO_INT.put(STR_LOCAL, INT_LOCAL);
        STR_TO_INT.put(STR_LEAN, INT_LEAN);
        STR_TO_INT.put(STR_NIM, INT_NIM);
        STR_TO_INT.put(STR_TIM, INT_TIM);
    }

    static {
        INT_TO_STR.put(INT_LOCAL, STR_LOCAL);
        INT_TO_STR.put(INT_LEAN, STR_LEAN);
        INT_TO_STR.put(INT_NIM, STR_NIM);
        INT_TO_STR.put(INT_TIM, STR_TIM);
    }

    @MessageIdTypeIntDef
    public static int strToInt(@MessageIdTypeStringDef String strType) {
        final Integer value = STR_TO_INT.get(strType);
        if (value == null) {
            throw new IllegalArgumentException(String.format("unsupported string type %s", strType));
        }
        return value;
    }

    @MessageIdTypeStringDef
    public static String intToStr(@MessageIdTypeIntDef int intType) {
        final String value = INT_TO_STR.get(intType);
        if (value == null) {
            throw new IllegalArgumentException(String.format("unsupported int type %s", intType));
        }
        return value;
    }
}
